﻿using System;
using System.Collections.Generic;

namespace TableTypes
{
    public class CancelledTableEntry
    {
        public ClassTime ClassTime = null;
        public ClassName ClassName = null;
        public String Course = null;
        public int Unit = 0;
    }

    public class CancelledTable: List<CancelledTableEntry>
    {
        public static CancelledTable Fetch(WebLayer.LoginToken loginToken)
        {
            WebParser.WebPageCache pageCache = new WebParser.WebPageCache();
            pageCache.LoginToken = loginToken;

            return Fetch(pageCache);
        }

        public static CancelledTable Fetch(WebParser.WebPageCache pageCache)
        {
            WebParser.ClassesWebPage webPage = pageCache.GetPage<WebParser.ClassesWebPage>();

            CancelledTable attendanceTable = new CancelledTable();
            foreach (WebParser.ClassesTableEntry webEntry in webPage.CancelledTableContent)
            {
                CancelledTableEntry memEntry = new CancelledTableEntry();

                memEntry.ClassName = ClassName.ParseClassName(webEntry.Type);
                memEntry.ClassTime = ClassTime.ParseCancelledEntry(webEntry.Date, webEntry.Begin, webEntry.End);
                memEntry.Course = webEntry.Course;

                if (Int32.TryParse(webEntry.Unit, out memEntry.Unit) == false)
                {
                    memEntry.Unit = 0;
                }
                
                attendanceTable.Add(memEntry);
            }

            return attendanceTable;
        }
    }
}
